<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:refund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:refund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:refund:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['com/ruoyi/business:records:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table border stripe :header-cell-style="rowClass" v-loading="loading" :data="recordsList" @selection-change="handleSelectionChange">
      <el-table-column :resizable="false" type="selection" width="55" align="center" />
      <el-table-column :resizable="false" label="序号" type="index" align="center" width="100" />
      <el-table-column :resizable="false" label="早餐费用（元/餐）" align="center" prop="breakfastFee" />
      <el-table-column :resizable="false" label="午餐费用（元/餐）" align="center" prop="lunchFee" />
      <el-table-column :resizable="false" label="晚餐费用（元/餐）" align="center" prop="dinnerFee" />
      <el-table-column :resizable="false" label="早餐总费用" align="center" prop="totalBreakfastFee" />
      <el-table-column :resizable="false" label="午餐总费用" align="center" prop="totalLunchFee" />
      <el-table-column :resizable="false" label="晚餐总费用" align="center" prop="totalDinnerFee" />
      <el-table-column :resizable="false" label="学期名称" align="center" prop="semesterName" />
      <el-table-column :resizable="false" label="学期开始日期" align="center" prop="startDate" />
      <el-table-column :resizable="false" label="学期结束日期" align="center" prop="endDate" />
      <el-table-column :resizable="false" label="状态" align="center" prop="isActive">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActive" :active-value="1" :inactive-value="0"
            @change="changeSwitch(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:refund:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:refund:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值缴费记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="早餐费用（元/餐）" prop="breakfastFee">
          <el-input v-model="form.breakfastFee" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入早餐费用" />
          <!-- <el-input v-model="form.breakfastFee" oninput="value=/^\d+\.?\d{0,2}$/.test(value) ? value : value.substring(0,value.length-1)" placeholder="请输入早餐费用" /> -->
        </el-form-item>
        <el-form-item label="午餐费用（元/餐）" prop="lunchFee">
          <el-input v-model="form.lunchFee" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入午餐费用" />
        </el-form-item>
        <el-form-item label="晚餐费用（元/餐）" prop="dinnerFee">
          <el-input v-model="form.dinnerFee" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入晚餐费用" />
        </el-form-item>
        <el-form-item label="早餐总费用"  prop="totalBreakfastFee">
          <el-input v-model="form.totalBreakfastFee" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入早餐总费用" />
        </el-form-item>
        <el-form-item label="午餐总费用"  prop="totalLunchFee">
          <el-input v-model="form.totalLunchFee" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入午餐总费用" />
        </el-form-item>
        <el-form-item label="晚餐总费用"  prop="totalDinnerFee">
          <el-input v-model="form.totalDinnerFee" oninput="value=value.replace(/[^\d]/g,'')"  placeholder="请输入晚餐总费用" />
        </el-form-item>
        <el-form-item label="学期名称" prop="semesterName">
          <el-input v-model="form.semesterName" placeholder="请输入学期名称" />
        </el-form-item>
        <el-form-item label="学期开始日期"  prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择学期开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学期结束日期"  prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择学期结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecords, getRecords, delRecords, addRecords, updateRecords,changeStatus } from "@/api/financialManagement/feeConfiguration.js";

export default {
  name: "Records",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中学期名称数组
      semesterNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值缴费记录表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
       // 查询参数
       queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单校验
      rules: {
        breakfastFees: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
          { type: 'number', message: '年龄必须为数字值'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //设置table标题样式
    rowClass ({ row, rowIndex }) {
      return 'font-size: 15px; background-color: #FAFAFA;'
    },
    // 修改状态
    changeSwitch(e){
      console.log("1111",e)
      let text = e.isActive === 1 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"此配置吗？').then(function() {
        return changeStatus(e.configId, e.isActive);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        e.isActive = e.isActive === 0 ? 1 : 0;
      });
    },
    /** 查询充值缴费记录列表 */
    getList() {
      this.loading = true;
      listRecords(this.queryParams).then(response => {
        console.log("response===============>",JSON.parse(JSON.stringify(response.rows)));
        this.recordsList = JSON.parse(JSON.stringify(response.rows));
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        breakfastFee: null,
        lunchFee: null,
        dinnerFee: null,
        totalBreakfastFee: null,
        totalLunchFee: null,
        totalDinnerFee: null,
        semesterName: null,
        startDate: null,
        endDate: null,
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.semesterNames=selection.map(item=>item.semesterName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加费用配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(row,"==++++====");
      const configId = row.configId || this.ids
      getRecords(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改费用配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null) {
            updateRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.configId || this.ids;
      const semesterNames=row.semesterName || this.semesterNames
      this.$modal.confirm('是否确认删除费用配置学期名称为"' + semesterNames + '"的数据项？').then(function() {
        return delRecords(configId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('com/ruoyi/business/records/export', {
        ...this.queryParams
      }, `records_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
